<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\PosOrder;
use App\Models\Admin\PosShop;
use Illuminate\Http\Request;
use App\Models\Admin\RuzhuMerchantBasic;
use App\Models\Admin\Order;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class MerchantController extends CommonController
{

    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();//检查是否登陆
        $this->model = new RuzhuMerchantBasic;
    }


    /**
     * banner图添加/修改
     */
    public function edit(Request $request)
    {
        if ($request->isMethod('post')) {


            $validator = \Validator::make($request->all(), [
                'merchant_name' => 'required',
                'short_name' => 'required',
                'merchant_address' => 'required',
                'service_phone' => 'required',
                'open_time' => 'required',
                'logo' => 'required',
            ], [
                'merchant_name.required' => '请填写店铺名称',
                'short_name.required' => '请填写简称',
                'merchant_address.required' => '请填写店铺详细地址',
                'service_phone.required' => '请填写服务电话',
                'open_time.required' => '请填写营业时间',
                'logo.required' => '请上传图片',
            ]);

            if ($validator->fails()) {
                $this->ajax_error($validator->errors()->first());
            }

            $data = $request->only('merchant_name', 'short_name', 'merchant_address', 'service_phone', 'open_time', 'logo');

            if (!$this->model->where(array('id' => $this->merchant_id))->update($data)) $this->ajax_error('操作失败，请重试');
            $this->ajax_success('提交成功');

        } else {
            $data = $this->model->find($this->merchant_id, ['ID', 'SHORT_NAME', 'MERCHANT_NAME', 'MERCHANT_ADDRESS', 'SERVICE_PHONE', 'OPEN_TIME', 'LOGO']);
            return view('admin.merchant.edit')->with(['data' => $data]);
        }

    }


    /**
     * 商家线下订单列表
     */
    public function orderList(Request $request)
    {
        //商家ID
        $sid = $_SESSION['admin_uid'];
        $orderModel = new Order();
        $order_no = $request->input('order_no');
        $pay_state = $request->input('pay_state');
        $order_list = $orderModel->get_lineorder_list($order_no, $pay_state, $sid, $request->input());
        if(request()->get('export')=='true'){
            PosOrder::exportFileNormal('手机支付订单列表导出'.date('Y-m-d'),[
                '序号','订单ID','买家手机号','下单时间','支付总金额','让利金额','存款金额','手续费','应结算金额'
            ],Order::parseToExcel($order_list));
        }
        return view('admin.order.order_list', ['data' => $order_list]);
    }

    /**
     * @param Request $request
     * 删除订单
     */
    public function orderDel(Request $request)
    {
        $id = $request->input('id');
        $result = DB::table('vshops_order')->where('id', $id)->delete();
        if ($result) {
            $this->ajax_success('删除成功', $result);
        }
        $this->ajax_error('删除失败', $result);
    }

    /**
     * POS机支付订单
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function posOrderList(Request $request)
    {
        $sid = $_SESSION['node_id'];
        $posOrderModel = new PosOrder();
        $baseInfo = PosShop::where('node_id',$sid)->first();
        if(empty($baseInfo)){
            $baseInfo=new \stdClass();
            $baseInfo->rebate=20;//默认20 2017-12-4 16:24:22
        }
        $order_list = $posOrderModel->getLists($sid, $request->input());
        if(request()->get('export')=='true'){
            PosOrder::exportFileNormal('POS机支付订单列表导出'.date('Y-m-d'),[
                '序号','订单ID','订单类型','买家手机号','下单时间','支付总金额','让利金额','存款金额','手续费','应结算金额'
            ],PosOrder::parseToExcel($order_list));
        }
        return view('admin.order.pos_order_list', ['data' => $order_list,'baseInfo'=>$baseInfo]);
    }


}